require "Common/define"
require "Logic/Config"
require "Common/functions"


TacticsManager = {};
local this = TacticsManager;
local tacticsConfig = nil;
local tacticsTable = {};
local curUseId =0;

function TacticsManager.init()
    tacticsConfig = G.dataTable["t_tactics"];
    tacticsTable = {};
    curUseId =0;

    local tempRestrainTable = {}; --临时存储克制的表

    for k,v in pairs(tacticsConfig) do
        local data = tacticsTable[v.f_TacticsId];

        if data ==nil then
            data = {};
           
            data.tacticsId = v.f_TacticsId; --阵法id
            data.name = v.f_Name --名字
            data.activate = decodeJsonStrToTabel(v.f_Activate); --激活条件
            data.maxLevel =0; --最高等级
            data.restrains = {}; --克制
            data.berestrains = {}; --被克制
            
            data.level =0; --阵法等级
            data.isActivate = false; -- 是否激活
            data.isUsing = false; --是否是在使用中
            data.realId =0; --对应的阵法唯一id
            data.icon = v.f_Icon; --图标

            tacticsTable[v.f_TacticsId] = data;
        end

        
        if data.maxLevel < v.f_Level then
            data.maxLevel = v.f_Level;
        end

        if v.f_Restrain~=nil and #data.restrains<=0 then --克制关系读取
            local restrainList = decodeJsonStrToTabel(v.f_Restrain);
            for i=1,#restrainList do
                local itemData = this.createRestrainData(restrainList[i],true,0);
                table.insert(data.restrains,itemData);
                
                local berestrainTacticsId = restrainList[i][2];

                if tacticsTable[berestrainTacticsId]~=nil then -- 被克制赋值
                    local itemData = this.createRestrainData(restrainList[i],false,data.tacticsId);
                    table.insert(tacticsTable[berestrainTacticsId].berestrains,itemData);
                else --否则存到临时表中
                    local itemData = this.createRestrainData(restrainList[i],false,data.tacticsId);
                    if tempRestrainTable[berestrainTacticsId] ==nil then
                        tempRestrainTable[berestrainTacticsId] ={};
                    end
                    table.insert(tempRestrainTable[berestrainTacticsId],itemData);
                end
            end

            --临时变量里存的被克制关系赋值
            if tempRestrainTable[data.tacticsId]~=nil and #tempRestrainTable[data.tacticsId]>0 then 
                for i=1,#tempRestrainTable[data.tacticsId] do
                    table.insert(data.berestrains,tempRestrainTable[data.tacticsId][i]);
                end
                tempRestrainTable[data.tacticsId] = {};
            end
        end
    end
end

function TacticsManager.createRestrainData(restrainSingle,isRestrain,tacticsId) --isRestrain true为克制，false为被克制，tacticsId是被克制时需要赋值的阵法id
    local itemData = {};
    itemData.type = restrainSingle[1]; --克制类型
    itemData.tactics =isRestrain and restrainSingle[2] or tacticsId; -- 阵法id
    itemData.value = restrainSingle[3]; -- 增加伤害数值(万分比)
    return itemData;
end

function TacticsManager.setTacticsInfoList(infoList,useId) --设置当前角色的阵法信息
    this.setTacticsUse(useId);
    for i=1,#infoList do
        this.setTacticsInfo(infoList[i]);
    end
end

function TacticsManager.setTacticsInfo(data) --单独设置阵法信息
    local tacticsId = data.tacticsId;
    local tacticsInfo = tacticsTable[tacticsId];
    if tacticsInfo~=nil then
        tacticsInfo.level = data.level;
        tacticsInfo.isActivate = tacticsInfo.level>0;
        tacticsInfo.realId = tacticsInfo.tacticsId+ tacticsInfo.level;
    end
end

function TacticsManager.setTacticsUse(tacticsId) --使用后更新阵法信息
    if tacticsTable[curUseId]~=nil then
        tacticsTable[curUseId].isUsing =false;
    end
    curUseId = tacticsId;
    if tacticsTable[curUseId]~=nil then
        tacticsTable[curUseId].isUsing =true;
    end

end


function TacticsManager.getTacticsConfigByLevel(tacticsId,level) -- 根据阵法id和等级获取配置
    local uid = tacticsId+level;
    return tacticsConfig[tostring(uid)];
end

function TacticsManager.getTacticsConfigByUid(uid)-- 根据唯一id获取配置
    return tacticsConfig[tostring(uid)];
end

function TacticsManager.getTacticsIdByUid(uid) -- 唯一id计算阵法id
    return Mathf.Floor(uid/10000)*10000;
end

function TacticsManager.getTacticsInfo(tacticsId) --根据阵法id获取阵法信息
    local tacticsInfo = tacticsTable[tacticsId];
    if tacticsInfo~=nil then
        return clone(tacticsInfo);
    end
    return nil;
end

function TacticsManager.getCurTactics() -- 当前使用的阵法信息
    local tacticsInfo = tacticsTable[curUseId];
    if tacticsInfo~=nil then
        if tacticsInfo.isUsing and tacticsInfo.isActivate then
            return clone(tacticsInfo);
        end
    end
    return nil;
end


function TacticsManager.getTacticsList()
    local list = {};
    for k,v in pairs(tacticsTable) do
        table.insert(list,v);
    end
    return list;
end

function TacticsManager.isReachLevelPremise(tacticsId) -- 阵法满足升级/激活前提
    local localTacticsInfo = tacticsTable[tacticsId];
    if localTacticsInfo == nil or localTacticsInfo.level>= localTacticsInfo.maxLevel then
        return false;
    end

    local localConfig = this.getTacticsConfigByLevel(tacticsId,localTacticsInfo.level+1);
    if localConfig == nil then
        return false;
    end
    
    local premises = decodeJsonStrToTabel(localConfig.f_Activate);

    for i=1,#premises do
        local premise = premises[i];
        local premiseType = premise[1];
        if premiseType== EnumConst.TacticsActivateType.Level then
            if logicMgr.HeroManager.GetRoleLevel() < premise[2] then
                return false;
            end
        elseif premiseType== EnumConst.TacticsActivateType.Duplicate then
            if logicMgr.MapManager.getMapId()< premise[2] then
                return false;
            end
        end
    end
    return true;
end

return TacticsManager